<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 搜索区 -->
      <el-form
        ref="questionsInfoForm"
        :model="questionsInfo"
        label-width="80px"
        inline
        class="searchForm"
      >
        <el-form-item label="关键字">
          <el-input v-model.trim="questionsInfo.keyword" placeholder="根据编号搜索"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="default" size="small" @click="clearSearch">清除</el-button>
          <el-button type="primary" size="small" @click="getList()">搜索</el-button>
        </el-form-item>
      </el-form>
      <!-- 提示区 -->
      <el-alert :title="'数据一共' + total + '条'" type="info" show-icon :closable="false"></el-alert>
      <el-table :data="randomQues" style="width: 100%" border>
        <el-table-column prop="id" label="编号" width="220"></el-table-column>
        <el-table-column label="题型" width="90">
          <template slot-scope="scope">{{ type[scope.row.questionType] }}</template>
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号" width="220">
          <template slot-scope="scope">
            <!-- {{scope.row.questionIDs}} -->
            <div
              v-for="(num, i) in scope.row.questionIDs"
              :key="i"
              @click="showRandomDialog(num.id)"
            >
              <el-link type="primary">{{ num.number }}</el-link>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="160"></el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)"></el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)"></el-table-column>
        <el-table-column prop="userName" label="录入人"></el-table-column>
        <el-table-column prop="id" label="操作" width="90">
          <template slot-scope="scope">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delRandom(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <page-tool
        class="pageNation"
        :total="total"
        :paginationPage="questionsInfo.page"
        :paginationPagesize="questionsInfo.pagesize"
        @pageSizeChange="onpageSizeChange"
        @pageChange="onpageChange"
      ></page-tool>
    </el-card>
    <question-preview
      :dialogTableVisible="dialogTableVisible"
      @close-dialog="dialogTableVisible = false"
      :id="ids"
    ></question-preview>
  </div>
</template>

<script>
import { randomsQuestions, delRandomsQuestions } from '@/api/hmmm/questions.js'
import QuestionPreview from '../components/questions-preview'
import pageTool from '../components/pageTool'
export default {
  data() {
    return {
      // 请求试题的参数
      questionsInfo: {
        page: 1,
        pagesize: 20,
        keyword: '',
      },
      total: 0,
      randomQues: [], // 试题数据
      dialogTableVisible: false, // 控制弹框的显示与隐藏
      ids: '', // 试题预览的弹框组件的id传值
      type: {
        '1': '单选',
        '2': '多选',
        '3': '简答',
      },
    }
  },
  components: {
    QuestionPreview,
    pageTool,
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      randomsQuestions(this.questionsInfo).then((item) => {
        // console.log(item.data);
        this.total = item.data.counts
        this.randomQues = item.data.items
      })
    },
    // 展示预览试题的dialog
    showRandomDialog(id) {
      // console.log(id);
      this.ids = id
      this.dialogTableVisible = true
    },
    // 删除试题
    delRandom(id) {
      // console.log(id);
      try {
        // console.log(213);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            await delRandomsQuestions({ id })
            this.$message.success('删除成功')
            this.getList()
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      } catch (error) {
        this.$message.error('删除失败')
      }
    },
    onpageSizeChange(val) {
      this.questionsInfo.pagesize = val
      this.getList()
    },
    onpageChange(val) {
      this.questionsInfo.page = val
      this.getList()
    },
    // 清除事件
    clearSearch() {
      this.questionsInfo = {}
      this.getList()
    }
  },
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  .searchForm {
    display: flex;
    justify-content: space-between;
  }
  .pageNation {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
  }
}
</style>
